package smsweb.servlet;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import smsweb.client.Util;
import smsweb.server.Sms;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DeliverServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2594478080493633614L;
		
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		DataInputStream in = new DataInputStream(
				(InputStream) req.getInputStream());
		String text = in.readUTF();
		PrintWriter out = resp.getWriter();
		out.print("100 OK");
		out.close();
		int index = text.lastIndexOf("||");
		int index2 = text.indexOf("|&&|");
		
		String strId = text.substring(index+2, index2);
		long id = Long.parseLong(strId);
		System.out.println(strId + " silinecek"); 
		PersistenceManager pm = Util.getPMF().getPersistenceManager();
		Key key = KeyFactory.createKey(Sms.class.getSimpleName(), id);
		Sms sms = pm.getObjectById(Sms.class, key);
		sms.setSent(true);
		pm.close();
		
		
	}

}
